import { Modal, Form, Input } from 'antd';
import React from 'react';

import type { AddItem } from '../data';

interface AddFormProps {
  onSubmit: (values: AddItem) => void;
  onCancel: (flag?: boolean) => void;
  modalVisible: boolean;
}

const formItemLayout = {
  labelCol: {
    xs: { span: 4 },
    sm: { span: 4 },
  },
  wrapperCol: {
    xs: { span: 20 },
    sm: { span: 20 },
  },
};

const AddForm: React.FC<AddFormProps> = (props) => {
  const [form] = Form.useForm();

  const {
    onSubmit: handleAdd,
    onCancel: handleAddModalVisible,
    modalVisible: addModalVisible,
  } = props;

  const onModalOk = async () => {
    const fieldsValue = (await form.validateFields()) as AddItem;
    handleAdd(fieldsValue);
  };

  return (
    <Modal
      title="新建客户角色"
      visible={addModalVisible}
      destroyOnClose
      width={600}
      onOk={() => onModalOk()}
      onCancel={() => handleAddModalVisible(false)}
      afterClose={() => handleAddModalVisible()}
      okText="确定"
      cancelText="取消"
    >
      <Form {...formItemLayout} form={form}>
        <Form.Item name="name" label="角色名称" rules={[{ required: true, message: '必填' }]}>
          <Input />
        </Form.Item>
      </Form>
    </Modal>
  );
};
export default AddForm;
